//
//  PopCatView.m
//  MindBox
//
//  Created by MS on 16-1-11.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "PopCatView.h"
#import "UIView+Frame.h"
#import "GSAdsView.h"
#import "PopCat_Snd_bannerView.h"
#import "PopCat_GoodsView.h"

#define GOODS_VIEW_WIDTH 280
@interface PopCatView ()<GsAdsViewDelegate>
@property (nonatomic,weak) GSAdsView * adsView;
@property (nonatomic,strong) NSMutableArray * adsArray;
@property (nonatomic,weak) PopCat_Snd_bannerView * sndView;
@property (nonatomic,weak) PopCat_GoodsView * goodsView;

@end

@implementation PopCatView

-(void)setPopCatData:(PopCatPage *)popCatData
{
    _popCatData = popCatData;
    
    for (PopCat_Banner * banner in popCatData.banner ) {
        [self.adsArray addObject:banner.image_url];
    }
    self.adsView.imagesArray = _adsArray;
    if (popCatData.snd_banner.count) {
        self.sndView.sndArray = popCatData.snd_banner;
    }
    
    self.goodsView.popCatData = popCatData;
    self.bottomView.title.text = popCatData.search_title;
    self.gift_selectView.itemsArray = popCatData.gift_cat_id;
    
    [self layoutSubviews];
    [self.collectionView reloadData];
}

-(NSMutableArray *)adsArray
{
    if (_adsArray==nil) {
        _adsArray = [NSMutableArray array];
    }
    return _adsArray;
}

-(GSAdsView *)adsView
{
    if (_adsView==nil) {
        
        GSAdsView * view = [self creatAdsView];
        _adsView = view;
        _adsView.frame = CGRectMake(0, 0, self.width, 120);
        _adsView.intervalTime = 5;
        _adsView.delegate = self;
    }
    return _adsView;
}

-(GSAdsView *)creatAdsView
{
    GSAdsView * view = [[GSAdsView alloc] init];
    [self.collectionView addSubview:view];
    view.pageTintColor = [UIColor whiteColor];
    view.currentPageTintColor = [UIColor redColor];
    return view;
}

-(PopCat_Snd_bannerView *)sndView
{
    if (_sndView==nil) {
        PopCat_Snd_bannerView * view = [PopCat_Snd_bannerView   popCat_Snd_bannerViewWithXib];
        [self.collectionView addSubview:view];
        _sndView = view;
        
        __weak typeof(self) weakSelf = self;
        [_sndView setPopCatSndBlock:^(NSInteger index) {
            if ([weakSelf.popViewdelegate respondsToSelector:@selector(popSndViewDidClick:snd_banner:index:)]) {
                
                [weakSelf.popViewdelegate popSndViewDidClick:weakSelf snd_banner:weakSelf.popCatData.snd_banner[index] index:index];
            }
            
        }];
        
    }
    return _sndView;
}

-(PopCat_GoodsView *)goodsView
{
    if (_goodsView==nil) {
        PopCat_GoodsView * view =[PopCat_GoodsView popCatGoodsViewWithXib];
        _goodsView = view;
        [self.collectionView addSubview:view];
        __weak typeof(self) weakSelf = self;
        [_goodsView setPopCatGoodsBlock:^(NSInteger index) {
            
            if ([weakSelf.popViewdelegate respondsToSelector:@selector(popGoodsViewDidClick:goods:index:)]) {
                [weakSelf.popViewdelegate popGoodsViewDidClick:self goods:weakSelf.popCatData.goods[index] index:index];
            }
        }];
        
    }
    return _goodsView;
}

-(void)selectViewDidClick:(SelectView *)selectView index:(NSInteger)index
{
    if ([self.popViewdelegate respondsToSelector:@selector(popCatSelectViewDidClick:selectItem:index:)]) {
        [_popViewdelegate popCatSelectViewDidClick:self selectItem:self.popCatData.gift_cat_id[index] index:index];
    }
    
    if (selectView.y>CGRectGetMaxY(self.bottomView.frame)) {
        [self.collectionView setContentOffset:CGPointMake(0, CGRectGetMaxY(self.bottomView.frame)) animated:YES];
    }

}

-(void)gsAdsViewTouch:(GSAdsView *)GSAdsView Index:(NSInteger)index
{
    if ([self.popViewdelegate respondsToSelector:@selector(popAdsViewDidClick:banner:index:)]) {
        [self.popViewdelegate popAdsViewDidClick:self banner:self.popCatData.banner[index] index:index];
    }
   
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if ([self.popViewdelegate respondsToSelector:@selector(popColletionViewDidClick:goods:index:)]) {
        
        [self.popViewdelegate popColletionViewDidClick:self goods:self.gift_ItemGoods[indexPath.item] index:indexPath.item];
    }
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
     _adsView.frame = CGRectMake(0, 0, self.width, 120);
    
    _goodsView.frame = CGRectMake(0, CGRectGetMaxY(_adsView.frame), self.width, GOODS_VIEW_WIDTH);
   
    
    if (_popCatData.snd_banner.count){
         _sndView.frame =CGRectMake(0, CGRectGetMaxY(_adsView.frame)+10, self.width, 70);
         _goodsView.frame = CGRectMake(0, CGRectGetMaxY(_sndView.frame)+10, self.width, GOODS_VIEW_WIDTH);
    }
    
     self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(_goodsView.frame), self.width, 30);
    self.gift_selectView.frame = CGRectMake(0, CGRectGetMaxY(self.bottomView.frame), self.width, 30);
}

@end
